/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import com.facebook.drawee.drawable.ScalingUtils;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ouertech.android.agm.lib.base.utils.UtilList;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Follow;
import com.simon.mengkou.data.bean.base.Image;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/18.
 * desc    : 用户界面图片适配器
 */
public class UserImageAdapter extends AbsAdapter<Follow> {
    private GenericDraweeHierarchyBuilder mBuilder;

    public UserImageAdapter(Context context) {
        super(context);
        mBuilder = new GenericDraweeHierarchyBuilder(mContext.getResources())
                .setFadeDuration(300)
                .setActualImageScaleType(ScalingUtils.ScaleType.CENTER_CROP);
    }

    @Override
    public int getCount() {
        if(UtilList.isNotEmpty(mList)) {
            int size = mList.size();
            if(size % 3 == 0) {
                return size / 3;
            } else {
                return  size / 3 + 1;
            }
        } else {
            return 0;
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        final Holder holder;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.layout_user_image_item, null);
            holder = new Holder();
            convertView.setTag(holder);
            ButterKnife.bind(holder, convertView);
        } else {
            holder = (Holder) convertView.getTag();
        }



        //图片
        if(position * 3 < mList.size()) {
            final Follow data = getItem(position * 3);
            holder.mSdvImage0.setVisibility(View.VISIBLE);

            if(UtilList.isNotEmpty(data.getImageArray())) {
                OuerApplication.mImageLoader.loadImage(holder.mSdvImage0,
                        data.getImageArray().get(0).getUrl(), mBuilder);
            } else {
                OuerApplication.mImageLoader.loadImage(holder.mSdvImage0,
                        data.getVideoCover(), mBuilder);
            }

            holder.mSdvImage0.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //todo
                    OuerDispatcher.presentShareOrderDetailActivity(mContext, data.getId());
                }
            });
        } else {
            holder.mSdvImage0.setVisibility(View.INVISIBLE);
        }

        if(position * 3 + 1 < mList.size()) {
            final Follow data = getItem(position * 3 + 1);
            holder.mSdvImage1.setVisibility(View.VISIBLE);
            if(UtilList.isNotEmpty(data.getImageArray())) {
                OuerApplication.mImageLoader.loadImage(holder.mSdvImage1,
                        data.getImageArray().get(0).getUrl(), mBuilder);
            } else {
                OuerApplication.mImageLoader.loadImage(holder.mSdvImage1,
                        data.getVideoCover(), mBuilder);
            }

            holder.mSdvImage1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //todo
                    OuerDispatcher.presentShareOrderDetailActivity(mContext, data.getId());
                }
            });
        } else {
            holder.mSdvImage1.setVisibility(View.INVISIBLE);
        }

        if(position * 3 + 2 < mList.size()) {
            final Follow data = getItem(position * 3 + 2);
            holder.mSdvImage2.setVisibility(View.VISIBLE);
            if(UtilList.isNotEmpty(data.getImageArray())) {
                OuerApplication.mImageLoader.loadImage(holder.mSdvImage2,
                        data.getImageArray().get(0).getUrl(), mBuilder);
            } else {
                OuerApplication.mImageLoader.loadImage(holder.mSdvImage2,
                        data.getVideoCover(), mBuilder);
            }
            holder.mSdvImage2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //todo
                    OuerDispatcher.presentShareOrderDetailActivity(mContext, data.getId());
                }
            });
        } else {
            holder.mSdvImage2.setVisibility(View.INVISIBLE);
        }

        return convertView;
    }


    /**
     * @author zhenshui.xia
     */
    class Holder {
        //图片
        @Bind(R.id.image_id_image0)
        SimpleDraweeView mSdvImage0;

        @Bind(R.id.image_id_image1)
        SimpleDraweeView mSdvImage1;

        @Bind(R.id.image_id_image2)
        SimpleDraweeView mSdvImage2;
    }
}
